class MusicPlayer {
  private var musicList: List[String] = List() // 音乐列表
  private var currentMusic: Option[String] = None
  private var playMode: String = "顺序播放"
  // 添加
  def addMusic(music: String): Unit = {
    musicList = musicList :+ music
    println(s"Added music: $music")
  }
  // 删除
  def removeMusic(music: String): Unit = {
    if (musicList.contains(music)) {
      musicList = musicList.filterNot(_ == music)
      println(s"删除: $music")
    } else {
      println(s"音乐 '$music' 不在列表里.")
    }
  }
  // 播放
  def playMusic(): Unit = {
    currentMusic match {
      case Some(music) => println(s"播放: $music")
      case None => println("没有音乐可播放.")
    }
  }
  // 播放下一首
  def nextMusic(): Unit = {
    musicList match {
      case Nil => println("没有音乐可播放.")
      case _ =>
        val currentIndex = musicList.indexOf(currentMusic.getOrElse(""))
        val nextIndex = if (currentIndex + 1 < musicList.length) currentIndex + 1 else 0
        currentMusic = Some(musicList(nextIndex))
        println(s"下一首: ${currentMusic.get}")
    }
  }
  // 播放上一首
  def previousMusic(): Unit = {
    musicList match {
      case Nil => println("没有音乐可播放.")
      case _ =>
        val currentIndex = musicList.indexOf(currentMusic.getOrElse(""))
        val prevIndex = if (currentIndex - 1 >= 0) currentIndex - 1 else musicList.length - 1
        currentMusic = Some(musicList(prevIndex))
        println(s"上一首: ${currentMusic.get}")
    }
  }
  // 设置播放模式
  def setPlayMode(mode: String): Unit = {
    playMode = mode
    println(s"设置模式: $playMode")
  }
  // 显示当前的音乐列表
  def displayMusicList(): Unit = {
    if (musicList.isEmpty) {
      println("没有音乐.")
    } else {
      println("音乐列表:")
      musicList.foreach(println)
    }
  }
  // 播放当前音乐信息
  def play(): Unit = {
    if (musicList.isEmpty) {
      println("No music in the playlist to play.")
    } else {
      println(s"Now playing: ${currentMusic.getOrElse("No music selected")}")
    }
  }
}
object Main extends App {
  // 创建
  val player = new MusicPlayer()
  player.addMusic("Song 1")
  player.addMusic("Song 2")
  player.addMusic("Song 3")
  player.displayMusicList()
  player.setPlayMode("随机播放")
  player.playMusic()
  player.nextMusic()  // 播放下一首
  player.previousMusic()  // 播放上一首
  player.removeMusic("Song 2")
  player.displayMusicList()
  player.playMusic()
}
